{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 0.矩阵及其运算\n",
    "## 0.1.环境、库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import itertools\n",
    "from functools import reduce\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体   \n",
    "plt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题\n",
    "x, y, z, t = sp.symbols('x y z t')\n",
    "k, m, n = sp.symbols('k m n', integer=True)\n",
    "f, g, h = sp.symbols('f g h', cls=sp.Function)\n",
    "sp.init_printing()\n",
    "# common tool function\n",
    "def getM(var_name,r,c):\n",
    "    \"\"\"\n",
    "    make a sympy s matrix\n",
    "    :param var_name:the symbol name\n",
    "    :param r:matrix s rows\n",
    "    :param c:matrix s cols\n",
    "    \"\"\"\n",
    "    if r>1 and c>1:\n",
    "        ss=sp.symbols(\"{0}_{{{{0:{1}}}{{0:{2}}}}}\".format(var_name,r,c))        \n",
    "    elif r==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,c))\n",
    "    elif c==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,r))\n",
    "    else:\n",
    "        raise Exception('invalid input parameter！')\n",
    "    return sp.Matrix([ss[i*c:(i+1)*c] for i in range(r)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "table {width:95%;}\n",
       "table td, table th, table tr {text-align:left !important;border: 1px  #888888 solid !important;}\n",
       "</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%HTML\n",
    "<style>\n",
    "table {width:95%;}\n",
    "table td, table th, table tr {text-align:left !important;border: 1px  #888888 solid !important;}\n",
    "</style>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.矩阵\n",
    "内容提要\n",
    ">1. 矩阵的定义\n",
    "2. 特殊的矩阵\n",
    "3. 矩阵与线性变换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1.定义\n",
    "数学上，一个$m*n$的矩阵是一个m行n列元素排列形成的矩形阵列，矩阵里的元素可以是数字、符号或者数学式。在图像处理、人工智能、量化投资等领域，使用矩阵来表示和处理数据非常常见。矩阵最早来自于方程组的系数及常数所构成的方阵。例如，如下方程组:\n",
    "> $\\begin{cases}\n",
    " & 4x_1-5x_2=-13 \\\\ \n",
    " &-2x_1+3x_2=9 \n",
    "\\end{cases}$\n",
    "\n",
    "这里有两个方程和两个变量，运用高中代数的知识，我们知道，可以为$x_1和x_2$找到一组唯一的解 (除非方程可以进一步简化，例如，如果第二个方程只是第一个方程的倍数形式。但是显然上面的例子不可简化，是有唯一解的)。在矩阵表达中，我们可以简洁的写作:\n",
    ">$Ax=b$\n",
    "\n",
    "其中:\n",
    ">$A=\\begin{bmatrix}4&-5\\\\-2&3\\end{bmatrix},b=\\begin{bmatrix}-13\\\\9\\end{bmatrix}$\n",
    "\n",
    "后面我们会看到，把方程表示成这种形式，在分析线性方程方面有很多优势(包括明显地节省空间)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**基本符号**\n",
    ">* $A\\in\\mathbb{R}^{m\\times n}$表示一个$m行n列$的矩阵，并且矩阵$A$中的所有元素都是实数，此时我们称$A$矩阵为实矩阵，如果元素是复数的矩阵，称为复矩阵;\n",
    "* $X\\in\\mathbb{R}^n$表示一个含有$n$个元素的向量。通常，我们把$n$维向量看成是一个$n行1列$矩阵，即列向量。如果我们想表示一个行向量（$1行n列$矩阵），我们通常写作$x^T$ ($x^T$表示$x$的转置，后面会解释它的定义);\n",
    "* 一个向量$X$的第i个元素表示为$x_i$:$X=\\begin{bmatrix}x_1\\\\x_2\\\\\\vdots\\\\x_n\\end{bmatrix}$;\n",
    "* $a_{ij}或者a_{i,j}$表示矩阵$A$的$第i行j列$的元素:$A=\\begin{bmatrix}a_{11}&\\dots&a_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{m1}&\\dots&a_{mn}\\end{bmatrix}$;\n",
    "* $a_{:,j}$表示$A$矩阵的第$j$列元素:$A=\\begin{bmatrix}a_{:,1}&\\dots&a_{:,n}\\end{bmatrix}$;\n",
    "* $a_{i,:}$表示$A$矩阵的第$i$行元素:$A=\\begin{bmatrix}a_{1,:}\\\\\\vdots\\\\a_{m,:}\\end{bmatrix}$\n",
    "\n",
    "基于上面的定义，矩阵$A$，简记为：$A=A_{m\\times n}={(a_{ij})}_{m \\times n}=(a_{ij})$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**行列式和矩阵的区别**\n",
    "\n",
    "|行列式|矩阵|\n",
    "| --- | --- |\n",
    "|$|A|$|$A$|\n",
    "|$\\begin{vmatrix}a_{11}&\\dots&a_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{n1}&\\dots&a_{nn}\\end{vmatrix}$|$\\begin{bmatrix}a_{11}&\\dots&a_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{m1}&\\dots&a_{mn}\\end{bmatrix}$|\n",
    "|<li>行数等于列数;</li><li>共有$n^2$个元素;</li>|<li>行数可以不等于列数;</li><li>共有$m\\times n$个元素;</li><li>本质上就是一个数表;</li>|\n",
    "|$det(a_{ij})$|${(a_{ij})}_{m\\times n}$|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2.特殊的矩阵\n",
    "1. 行数和列数都等于$n$的矩阵，称为**$n$阶方阵**,可记作$A_n$.\n",
    "2. 只有一行的矩阵$A=(a_1,\\dots,a_n)$称为**行矩阵**(或**行向量**).只有一列的矩阵$B=\\begin{pmatrix}a_1\\\\\\vdots\\\\a_n\\end{pmatrix}$称为**列矩阵**(或**列向量**).\n",
    "3. 元素全为零的矩阵称为**零矩阵**.可记作$O$.\n",
    "4. 除对角线外全为零的方阵称为**对角阵**.记作$A=diag(\\lambda_1,\\dots,\\lambda_n)$.特别的,对角线全为1的对角阵称为**单位阵**.记作$E_n$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**同型矩阵与矩阵相等的概念**\n",
    "1. 两个矩阵的行数相等、列数相等时,称为**同型矩阵**.\n",
    "2. 两个矩阵$A=(a_{ij})与B=(b_{ij})$为同型矩阵,并且对应元素相等,即$a_{ij}=b_{ij}$,则称矩阵$A与B$相等,记作$A=B$.**注意**：不同型的零矩阵是不相等的."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3.矩阵与线性变换\n",
    "$n个变量x_1,\\dots,x_n与m个变量y_1,\\dots,y_m$之间的关系式:<br>\n",
    "$\\begin{cases}y_1=a_{11}x_1+\\dots+a_{1n}x_n\\\\\\vdots\\\\y_m=a_{m1}x_1+\\dots+a_{mn}x_n \\end{cases}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}y_{0}\\\\y_{1}\\\\y_{2}\\end{matrix}\\right] = \\left[\\begin{matrix}a_{{0}{0}} x_{0} + a_{{0}{1}} x_{1} + a_{{0}{2}} x_{2}\\\\a_{{1}{0}} x_{0} + a_{{1}{1}} x_{1} + a_{{1}{2}} x_{2}\\\\a_{{2}{0}} x_{0} + a_{{2}{1}} x_{1} + a_{{2}{2}} x_{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡y_{0}⎤   ⎡a_{{0}{0}}⋅x_{0} + a_{{0}{1}}⋅x_{1} + a_{{0}{2}}⋅x_{2}⎤\n",
       "⎢     ⎥   ⎢                                                      ⎥\n",
       "⎢y_{1}⎥ = ⎢a_{{1}{0}}⋅x_{0} + a_{{1}{1}}⋅x_{1} + a_{{1}{2}}⋅x_{2}⎥\n",
       "⎢     ⎥   ⎢                                                      ⎥\n",
       "⎣y_{2}⎦   ⎣a_{{2}{0}}⋅x_{0} + a_{{2}{1}}⋅x_{1} + a_{{2}{2}}⋅x_{2}⎦"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xa,xx,xy=getM('a',xn,xn),getM('x',xn,1),getM('y',xn,1)\n",
    "sp.Eq(xy,xa*xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "表示一个从变量$x_1,\\dots,x_n到变量y_1,\\dots,y_m$的线性变换,其中$a_{ij}$为常数.\n",
    "\n",
    "从上面的关系式中，能够得到系数矩阵：<br>\n",
    "$A=\\begin{bmatrix}a_{11}&\\dots&a_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{m1}&\\dots&a_{mn}\\end{bmatrix}$<br>\n",
    "能够看到线性变换与矩阵之间存在着一一对应关系."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤\n",
       "⎢                                  ⎥\n",
       "⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥\n",
       "⎢                                  ⎥\n",
       "⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**恒等变换**，系数矩阵$A$为单位矩阵$E_n$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}1 & 0 & 0\\\\0 & 1 & 0\\\\0 & 0 & 1\\end{matrix}\\right], \\quad \\left[\\begin{matrix}y_{0}\\\\y_{1}\\\\y_{2}\\end{matrix}\\right] = \\left[\\begin{matrix}x_{0}\\\\x_{1}\\\\x_{2}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡1  0  0⎤  ⎡y_{0}⎤   ⎡x_{0}⎤⎞\n",
       "⎜⎢       ⎥  ⎢     ⎥   ⎢     ⎥⎟\n",
       "⎜⎢0  1  0⎥, ⎢y_{1}⎥ = ⎢x_{1}⎥⎟\n",
       "⎜⎢       ⎥  ⎢     ⎥   ⎢     ⎥⎟\n",
       "⎝⎣0  0  1⎦  ⎣y_{2}⎦   ⎣x_{2}⎦⎠"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.eye(xn)\n",
    "xa,sp.Eq(xy,xa*xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.矩阵的运算\n",
    "## 2.1.加法\n",
    "矩阵加法的定义非常复合直觉认识，和算术加法相似。定义：两个$m\\times n$矩阵$A=(a_{ij}),B=(b_{ij})$,那么矩阵$A与B$的和记作$A+B$，规定如下：<br>$A+B=\\begin{bmatrix}a_{11}+b_{11}&\\dots&a_{1n}+b_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{m1}+b_{m1}&\\dots&a_{mn}+b_{mn}\\end{bmatrix}$<br>\n",
    "**注意**：只有两个矩阵是同型矩阵时，才能进行加法运算.\n",
    "\n",
    "矩阵加法运算的规律：\n",
    "<table>\n",
    "    <tr><th></th><th>$\\forall a,b,c \\in \\mathbb R$</th><th>$A、B、C是同型矩阵$</th></tr>\n",
    "    <tr><td>交换律</td><td>$a+b=b+a$</td><td>$A+B=B+A$</td></tr>\n",
    "    <tr><td>结合律</td><td>$(a+b)+c=a+(b+c)$</td><td>$(A+B)+C=A+(B+C)$</td></tr>\n",
    "    <tr><td>其他</td><td colspan=2>设矩阵$A=(a_{ij}),记-A=(-a_{ij})$,称为矩阵$A$的**负矩阵**.显然$A+(-A)=0,A-B=A+(-B)$</td></tr>\n",
    "<table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} + b_{{0}{0}} & a_{{0}{1}} + b_{{0}{1}} & a_{{0}{2}} + b_{{0}{2}}\\\\a_{{1}{0}} + b_{{1}{0}} & a_{{1}{1}} + b_{{1}{1}} & a_{{1}{2}} + b_{{1}{2}}\\\\a_{{2}{0}} + b_{{2}{0}} & a_{{2}{1}} + b_{{2}{1}} & a_{{2}{2}} + b_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}a_{{0}{0}} + b_{{0}{0}} & a_{{0}{1}} + b_{{0}{1}} & a_{{0}{2}} + b_{{0}{2}}\\\\a_{{1}{0}} + b_{{1}{0}} & a_{{1}{1}} + b_{{1}{1}} & a_{{1}{2}} + b_{{1}{2}}\\\\a_{{2}{0}} + b_{{2}{0}} & a_{{2}{1}} + b_{{2}{1}} & a_{{2}{2}} + b_{{2}{2}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}} + b_{{0}{0}}  a_{{0}{1}} + b_{{0}{1}}  a_{{0}{2}} + b_{{0}{2}}⎤  \n",
       "⎜⎢                                                                         ⎥  \n",
       "⎜⎢a_{{1}{0}} + b_{{1}{0}}  a_{{1}{1}} + b_{{1}{1}}  a_{{1}{2}} + b_{{1}{2}}⎥, \n",
       "⎜⎢                                                                         ⎥  \n",
       "⎝⎣a_{{2}{0}} + b_{{2}{0}}  a_{{2}{1}} + b_{{2}{1}}  a_{{2}{2}} + b_{{2}{2}}⎦  \n",
       "\n",
       "⎡a_{{0}{0}} + b_{{0}{0}}  a_{{0}{1}} + b_{{0}{1}}  a_{{0}{2}} + b_{{0}{2}}⎤⎞\n",
       "⎢                                                                         ⎥⎟\n",
       "⎢a_{{1}{0}} + b_{{1}{0}}  a_{{1}{1}} + b_{{1}{1}}  a_{{1}{2}} + b_{{1}{2}}⎥⎟\n",
       "⎢                                                                         ⎥⎟\n",
       "⎣a_{{2}{0}} + b_{{2}{0}}  a_{{2}{1}} + b_{{2}{1}}  a_{{2}{2}} + b_{{2}{2}}⎦⎠"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa,xb,xc=[getM(i,xn,xn) for i in 'abc']\n",
    "xa+xb,xb+xa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa+xb==xb+xa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(xa+xb)+xc==xa+(xb+xc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2.减法\n",
    "矩阵减法也和加法一样简单。$A_{i,j}-B_{i,j}=\\begin{bmatrix}a_{11}-b_{11}&\\dots&a_{1n}-b_{1n}\\\\\\vdots&\\ddots&\\vdots\\\\a_{m1}-b_{m1}&\\dots&a_{mn}-b_{mn}\\end{bmatrix}$。对于上面给出的矩阵，有："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa-xb==xa+(-xb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3.数与矩阵相乘\n",
    "**定义**：数$\\lambda$与矩阵$A$的乘积记作**$\\lambda A$或者$A\\lambda$**,如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}a_{{0}{0}} x & a_{{0}{1}} x & a_{{0}{2}} x\\\\a_{{1}{0}} x & a_{{1}{1}} x & a_{{1}{2}} x\\\\a_{{2}{0}} x & a_{{2}{1}} x & a_{{2}{2}} x\\end{matrix}\\right], \\quad \\left[\\begin{matrix}a_{{0}{0}} x & a_{{0}{1}} x & a_{{0}{2}} x\\\\a_{{1}{0}} x & a_{{1}{1}} x & a_{{1}{2}} x\\\\a_{{2}{0}} x & a_{{2}{1}} x & a_{{2}{2}} x\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤  ⎡a_{{0}{0}}⋅x  a_{{0}{1}}⋅x  a_{{0}{2}}\n",
       "⎜⎢                                  ⎥  ⎢                                      \n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥, ⎢a_{{1}{0}}⋅x  a_{{1}{1}}⋅x  a_{{1}{2}}\n",
       "⎜⎢                                  ⎥  ⎢                                      \n",
       "⎝⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦  ⎣a_{{2}{0}}⋅x  a_{{2}{1}}⋅x  a_{{2}{2}}\n",
       "\n",
       "⋅x⎤  ⎡a_{{0}{0}}⋅x  a_{{0}{1}}⋅x  a_{{0}{2}}⋅x⎤⎞\n",
       "  ⎥  ⎢                                        ⎥⎟\n",
       "⋅x⎥, ⎢a_{{1}{0}}⋅x  a_{{1}{1}}⋅x  a_{{1}{2}}⋅x⎥⎟\n",
       "  ⎥  ⎢                                        ⎥⎟\n",
       "⋅x⎦  ⎣a_{{2}{0}}⋅x  a_{{2}{1}}⋅x  a_{{2}{2}}⋅x⎦⎠"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa,xa*x,x*xa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa*x==x*xa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数乘矩阵的运算规律：\n",
    "<table>\n",
    "    <tr><th></th><th>$\\forall a,b,c \\in \\mathbb R$</th><th>$A、B是同型矩阵,\\lambda,\\mu 是数$</th></tr>\n",
    "    <tr><td>结合律</td><td>$abc=a(bc)$</td><td>$(\\lambda\\mu)A=\\lambda(\\mu A)$</td></tr>\n",
    "    <tr>\n",
    "        <td>分配律</td><td>$(a+b)\\cdot c=ac+bc$<br>$c \\cdot (a+b)=ca+cb$</td>\n",
    "        <td>$(\\lambda+\\mu)A=\\lambda A+\\mu A$<br>$\\lambda(A+B)=\\lambda A+\\lambda B$</td>\n",
    "    </tr>\n",
    "    <tr><td>其他</td><td colspan=2>矩阵相加与相乘结合起来，统称为**矩阵的线性运算**</td></tr>\n",
    "<table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x*y*xa==x*(y*xa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify((x+y)*xa-(x*xa+y*xa))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(x*(xa+xb)-x*xa-x*xb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.4.矩阵与矩阵相乘\n",
    "**定义：**$A_{i,j}$矩阵的每一行的元素分别与$B_{j,k}$矩阵的每一列的元素两两相乘并相加，从而得到一个新的矩阵$C_{i,k}$。两个矩阵能相乘的充分必要条件是第一个矩阵的列数与第二个矩阵的行数相等，否则无定义。$C_{i,k}=A_{i,j}\\times B_{j,k},c_{i,j}=\\sum_{n=0}^{k-1}(a_{i,n}\\times b_{n,j})$。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}b_{{0}{0}} & b_{{0}{1}} & b_{{0}{2}}\\\\b_{{1}{0}} & b_{{1}{1}} & b_{{1}{2}}\\\\b_{{2}{0}} & b_{{2}{1}} & b_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}c_{{0}{0}} & c_{{0}{1}} & c_{{0}{2}}\\\\c_{{1}{0}} & c_{{1}{1}} & c_{{1}{2}}\\\\c_{{2}{0}} & c_{{2}{1}} & c_{{2}{2}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤  ⎡b_{{0}{0}}  b_{{0}{1}}  b_{{0}{2}}⎤  ⎡\n",
       "⎜⎢                                  ⎥  ⎢                                  ⎥  ⎢\n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥, ⎢b_{{1}{0}}  b_{{1}{1}}  b_{{1}{2}}⎥, ⎢\n",
       "⎜⎢                                  ⎥  ⎢                                  ⎥  ⎢\n",
       "⎝⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦  ⎣b_{{2}{0}}  b_{{2}{1}}  b_{{2}{2}}⎦  ⎣\n",
       "\n",
       "c_{{0}{0}}  c_{{0}{1}}  c_{{0}{2}}⎤⎞\n",
       "                                  ⎥⎟\n",
       "c_{{1}{0}}  c_{{1}{1}}  c_{{1}{2}}⎥⎟\n",
       "                                  ⎥⎟\n",
       "c_{{2}{0}}  c_{{2}{1}}  c_{{2}{2}}⎦⎠"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa,xb,xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} b_{{0}{0}} + a_{{0}{1}} b_{{1}{0}} + a_{{0}{2}} b_{{2}{0}} & a_{{0}{0}} b_{{0}{1}} + a_{{0}{1}} b_{{1}{1}} + a_{{0}{2}} b_{{2}{1}} & a_{{0}{0}} b_{{0}{2}} + a_{{0}{1}} b_{{1}{2}} + a_{{0}{2}} b_{{2}{2}}\\\\a_{{1}{0}} b_{{0}{0}} + a_{{1}{1}} b_{{1}{0}} + a_{{1}{2}} b_{{2}{0}} & a_{{1}{0}} b_{{0}{1}} + a_{{1}{1}} b_{{1}{1}} + a_{{1}{2}} b_{{2}{1}} & a_{{1}{0}} b_{{0}{2}} + a_{{1}{1}} b_{{1}{2}} + a_{{1}{2}} b_{{2}{2}}\\\\a_{{2}{0}} b_{{0}{0}} + a_{{2}{1}} b_{{1}{0}} + a_{{2}{2}} b_{{2}{0}} & a_{{2}{0}} b_{{0}{1}} + a_{{2}{1}} b_{{1}{1}} + a_{{2}{2}} b_{{2}{1}} & a_{{2}{0}} b_{{0}{2}} + a_{{2}{1}} b_{{1}{2}} + a_{{2}{2}} b_{{2}{2}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}⋅b_{{0}{0}} + a_{{0}{1}}⋅b_{{1}{0}} + a_{{0}{2}}⋅b_{{2}{0}}  a_{{0}\n",
       "⎢                                                                             \n",
       "⎢a_{{1}{0}}⋅b_{{0}{0}} + a_{{1}{1}}⋅b_{{1}{0}} + a_{{1}{2}}⋅b_{{2}{0}}  a_{{1}\n",
       "⎢                                                                             \n",
       "⎣a_{{2}{0}}⋅b_{{0}{0}} + a_{{2}{1}}⋅b_{{1}{0}} + a_{{2}{2}}⋅b_{{2}{0}}  a_{{2}\n",
       "\n",
       "{0}}⋅b_{{0}{1}} + a_{{0}{1}}⋅b_{{1}{1}} + a_{{0}{2}}⋅b_{{2}{1}}  a_{{0}{0}}⋅b_\n",
       "                                                                              \n",
       "{0}}⋅b_{{0}{1}} + a_{{1}{1}}⋅b_{{1}{1}} + a_{{1}{2}}⋅b_{{2}{1}}  a_{{1}{0}}⋅b_\n",
       "                                                                              \n",
       "{0}}⋅b_{{0}{1}} + a_{{2}{1}}⋅b_{{1}{1}} + a_{{2}{2}}⋅b_{{2}{1}}  a_{{2}{0}}⋅b_\n",
       "\n",
       "{{0}{2}} + a_{{0}{1}}⋅b_{{1}{2}} + a_{{0}{2}}⋅b_{{2}{2}}⎤\n",
       "                                                        ⎥\n",
       "{{0}{2}} + a_{{1}{1}}⋅b_{{1}{2}} + a_{{1}{2}}⋅b_{{2}{2}}⎥\n",
       "                                                        ⎥\n",
       "{{0}{2}} + a_{{2}{1}}⋅b_{{1}{2}} + a_{{2}{2}}⋅b_{{2}{2}}⎦"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa*xb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵的乘积记作：$C=AB$\n",
    "\n",
    "一般情况下，矩阵乘法不满足交换律"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa*xb==xb*xa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另外，$A\\not=O,B\\not=O,可能出现AB=O$,而$AB=O,不一定A=O或者B=O$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**矩阵乘法的运算规律**\n",
    "1. 乘法结合律"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa*xb*xc-xa*(xb*xc))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 数乘和乘法的结合律"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(x*xa*xb-x*(xa*xb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 乘法对加法的的分配率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right], \\quad \\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡0  0  0⎤  ⎡0  0  0⎤⎞\n",
       "⎜⎢       ⎥  ⎢       ⎥⎟\n",
       "⎜⎢0  0  0⎥, ⎢0  0  0⎥⎟\n",
       "⎜⎢       ⎥  ⎢       ⎥⎟\n",
       "⎝⎣0  0  0⎦  ⎣0  0  0⎦⎠"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa*(xb+xc)-(xa*xb+xa*xc)),sp.simplify(xa*(xb+xc)-(xa*xb+xa*xc))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 单位矩阵在矩阵乘法中的作用类似于数1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa==xa*sp.eye(xn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 矩阵的幂\n",
    "\n",
    "若$A$是$n$阶方阵,定义$A^k=\\overset{\\underbrace{A\\dots A}}{k}$,显然,$A^kA^l=A^{k+l},{(A^k)}^l=A^{lk}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa**2*xa**3-xa**(2+3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.expand((xa**2)**3-xa**(2*3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.5.矩阵的转置\n",
    "**定义：**把矩阵$A$的行换成同序数的列得到的新矩阵，叫做矩阵$A$的**转置矩阵**,记作$A^T$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}}\\\\a_{{1}{0}} & a_{{1}{1}}\\\\a_{{2}{0}} & a_{{2}{1}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}a_{{0}{0}} & a_{{1}{0}} & a_{{2}{0}}\\\\a_{{0}{1}} & a_{{1}{1}} & a_{{2}{1}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}⎤                                      ⎞\n",
       "⎜⎢                      ⎥  ⎡a_{{0}{0}}  a_{{1}{0}}  a_{{2}{0}}⎤⎟\n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}⎥, ⎢                                  ⎥⎟\n",
       "⎜⎢                      ⎥  ⎣a_{{0}{1}}  a_{{1}{1}}  a_{{2}{1}}⎦⎟\n",
       "⎝⎣a_{{2}{0}}  a_{{2}{1}}⎦                                      ⎠"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa,xb=[getM(i,3,2) for i in 'ab']\n",
    "xa,xa.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "转置矩阵的运算性质:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa==xa.T.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(xa+xb).T==xa.T+xb.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x*xa).T==x*xa.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(xa*xb.T).T==xb.T.T*xa.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义：**$A$为$n$阶方阵，如果满足$A=A^T$,那么矩阵$A$称为**对称阵**.如果满足$A=-A^T$,那么矩阵$A$称为**反对称阵**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤\n",
       "⎢                                  ⎥\n",
       "⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥\n",
       "⎢                                  ⎥\n",
       "⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=getM('a',xn,xn)\n",
    "xa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAATgAAAAWCAYAAABUroCYAAAABHNCSVQICAgIfAhkiAAABUBJREFUeJztnF2sXUMUx39XUZKLFkFRhBCtlj7RBM0JHog0PpvSByEhok1IxAMP4kbQpHKTq4mP1IMt6gEPCBJefCSkvaFNRUKVROsrrZJqfbTc1vGwZueezt1n7zX7Y86c0/klO+fePWvP/q+zZtaeMzPnQCQSiRyiLAPWA7uBA8CC3sqJRCIRAEaACWA78AZwXpbR4TkVzAXWAn8BrwE/mcoikUik13wIHIUMuq4DTgUudqlgBdAG7q1bWSQSidTIBuA/YNguOCznouPN61dNKIpEIpGa2AwMATPtgrwEN828ThRUvgx4GdgC/AHsAj4F7nCW6Y9+1FyVfvS5HzVXoR/9DUFzmqOm2QVTTnTQMseLwNYuNscgixB7kWHiB8D3wELgVmAf8Im73lIkwOvANmBTjl1Imn0Ris8JuhhBOJp9EZK/Cf3Vl65H5uKeAn7XXjSGzMFdkmMzDJyScX4Wks036zVWJkH03l5gF5JmX4Tic4IuRhCOZl+E5G9Cf/Wl5xC9F2kvGGJy4i7LAQ1fA7+UvLYMs4DzgeMq1OFbcwj49LmOGMGhF6fYl/J5EElw9xUZXg2MIp+h20hmzGMm8DAyTN2F7JVrdxwbLfvlwHfI0HUDcLlVvgh4C/jZXH9zkeASuGrWairyrZfUHaeHkDayB9iJvD/zeqhZq2dQYqRpk6HFSKupTA44DZlGmwBeBR4HzsoyTD+WtoEvgAtyKr0Q2RfXBsaBZ03FI8i8XRt4ocN+qRFwFzAHWA38CZzRYXMN8Bhwo4NzLrhq1mrS+NYrmojTe8gk8jxgPjJfs53JlXffmjV6BilGmjYZWoy0msrmgLuBv5nMX61uhsciG+f2Aj/SfSHic+RJmFXRo+YmKzrOjQPPW3bfACu71N9EgnPVrNXk6ptPmo4TyFzMAWBxaZUHUzVOWXoGKUadaPtJaDHSaNL6dq2x/RgZlB2huIa15qKsYe1sU/ZuRtkM4AdTvtCcOxLYDyyxbJ8GPupy/7oTnKtmraYyvvnCR5xA5mzawKVVxBrqiJOtZ5BiZKPtJ6HFSKNJ69uosb3KLsjbB7fNvGYNafeZ17M5OFueALwCnI40qHSJ+URkJLjDqmcH5RcxbIomRl01a3HxLUG/ilgHvuI0ZupYX6BHM3ldR5xsPa5+JfiLU1Pt0kYbI/DXl1w05ZFu8N1qF+QluIkcm53A+8C5yNB/FfASsnKyB1l9/ZLJNyKlbf0/lHGuLCuRb13c0KW8rGYtGt/S93J/yXu44iNOTyITw0uQjxt5FMWoimaNHq1fPuPUdLsEtxiBn77kqimPrvHKS3BFiecWxKnZwD3AOcD9wBOm3s86bH9FnLCflicx9anaJC6atbj4Nh/ZH/ROifuUpck4jQK3AVcC3/ZIs0aPq1++49REu0wJLUZNanIaLKV7S/Keti6MA2usc1vwu8hQlbxFhiLfZiCdbFUz0mpDG6fVSHKY60OUgiI9Wr/6JU4pef0ktBiBmyZtDnjT2J7sIuQmc9HbSJbOG+1pWAr8C9yJLNOPIcv0Z3bYDCNfuVhg7v2A+buXS/kaTRrfFiND9rrmHJtC48szyEeRKxB/0mPKrzl4QqNH4xf0R5w0bTK0GGk1ueSA6chPJO02h1OOOhrZC9e5ca/qD14uRyYC/0E2Wi6yylvW/dIjqXjfKrQy9GRpKvKtnyjyJev9aCN7oHqBVs+gxKhFcZsMLUZaTa0uNolV14hV/kjWDYcKBE1Hsu0cJLOuIf7oZSQS6T0t4DLgN2Ad1VeZI5FIJBKJRCJB8D9OHdMgeTtkWgAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$$\\left \\{ a_{{0}{1}} : a_{{1}{0}}, \\quad a_{{0}{2}} : a_{{2}{0}}, \\quad a_{{1}{2}} : a_{{2}{1}}\\right \\}$$"
      ],
      "text/plain": [
       "{a_{{0}{1}}: a_{{1}{0}}, a_{{0}{2}}: a_{{2}{0}}, a_{{1}{2}}: a_{{2}{1}}}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve(xa-xa.T,xa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/latex": [
       "$$\\left \\{ a_{{0}{0}} : 0, \\quad a_{{0}{1}} : - a_{{1}{0}}, \\quad a_{{0}{2}} : - a_{{2}{0}}, \\quad a_{{1}{1}} : 0, \\quad a_{{1}{2}} : - a_{{2}{1}}, \\quad a_{{2}{2}} : 0\\right \\}$$"
      ],
      "text/plain": [
       "{a_{{0}{0}}: 0, a_{{0}{1}}: -a_{{1}{0}}, a_{{0}{2}}: -a_{{2}{0}}, a_{{1}{1}}: \n",
       "0, a_{{1}{2}}: -a_{{2}{1}}, a_{{2}{2}}: 0}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve(xa+xa.T,xa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.6.方阵的行列式\n",
    "**定义:**由$n$阶方阵的元素所构成的行列式，叫做**方阵$A$的行列式**,记作$|A|或det(A)$.\n",
    "\n",
    "**运算性质**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "xn=3\n",
    "xa,xb=[getM(i,xn,xn) for i in 'ab']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.方阵的行列式与方阵的转置的行列式相等\n",
    "xa.T.det()==xa.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAABHNCSVQICAgIfAhkiAAAAM5JREFUOI3N0rEyQ0EUxvEfJcMVWhoZ8Qpq75PnoDJ0Kl5BK0U67yAUiUYmpZGYoRDNuePOkXszYxS+5ps95/x3Z3c//lC7uMIz3jHCOVpNUBsTzHGDU/RjfY+dOvA2hrqpfhb1y0XQfjSHWE29DUwxw7o0cBzew2cCX3GHNRxl8DD8oeYaj+GdDBbhLzVgWd/K4DKthM8zWO5YWKzN6lwVHIR3asCD8B9v0Lb8O97Ed2T9KgDlqdXInfiO3EBD5GAP1xjjA0+4wHYT9I/1BRgnMa7SDNsAAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$0$$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.数乘矩阵的行列式与数的阶次幂、乘矩阵的行列式相等\n",
    "sp.simplify((x*xa).det()-x**xn*xa.det())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAABHNCSVQICAgIfAhkiAAAAM5JREFUOI3N0rEyQ0EUxvEfJcMVWhoZ8Qpq75PnoDJ0Kl5BK0U67yAUiUYmpZGYoRDNuePOkXszYxS+5ps95/x3Z3c//lC7uMIz3jHCOVpNUBsTzHGDU/RjfY+dOvA2hrqpfhb1y0XQfjSHWE29DUwxw7o0cBzew2cCX3GHNRxl8DD8oeYaj+GdDBbhLzVgWd/K4DKthM8zWO5YWKzN6lwVHIR3asCD8B9v0Lb8O97Ed2T9KgDlqdXInfiO3EBD5GAP1xjjA0+4wHYT9I/1BRgnMa7SDNsAAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$0$$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.矩阵相乘之后的行列式与矩阵行列式的乘积相等\n",
    "sp.simplify((xa*xb).det()-xa.det()*xb.det())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAABHNCSVQICAgIfAhkiAAAAM5JREFUOI3N0rEyQ0EUxvEfJcMVWhoZ8Qpq75PnoDJ0Kl5BK0U67yAUiUYmpZGYoRDNuePOkXszYxS+5ps95/x3Z3c//lC7uMIz3jHCOVpNUBsTzHGDU/RjfY+dOvA2hrqpfhb1y0XQfjSHWE29DUwxw7o0cBzew2cCX3GHNRxl8DD8oeYaj+GdDBbhLzVgWd/K4DKthM8zWO5YWKzN6lwVHIR3asCD8B9v0Lb8O97Ed2T9KgDlqdXInfiO3EBD5GAP1xjjA0+4wHYT9I/1BRgnMa7SDNsAAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$0$$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.(A与B的乘积)的行列式与(B与A的乘积)的行列式相等\n",
    "sp.simplify((xa*xb).det()-(xb*xa).det())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**行列式$|A|$的各个元素的代数余子式$A_{ij}$所构成的如下矩阵：<br>\n",
    "$A^*=\\begin{bmatrix}A_{11}&\\dots&A_{n1}\\\\\\vdots&\\ddots&\\vdots\\\\A_{1n}&\\dots&A_{nn}\\end{bmatrix}$\n",
    "称为矩阵$A$的伴随矩阵.其中元素$a_{ij}$的代数余子式$A_{ij}$位于第**$j行i列$(注意)**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**性质:**$AA^*=A^*A=|A|E$.(通过行列式性质容易证明)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}a_{{1}{1}} a_{{2}{2}} - a_{{1}{2}} a_{{2}{1}} & - a_{{0}{1}} a_{{2}{2}} + a_{{0}{2}} a_{{2}{1}} & a_{{0}{1}} a_{{1}{2}} - a_{{0}{2}} a_{{1}{1}}\\\\- a_{{1}{0}} a_{{2}{2}} + a_{{1}{2}} a_{{2}{0}} & a_{{0}{0}} a_{{2}{2}} - a_{{0}{2}} a_{{2}{0}} & - a_{{0}{0}} a_{{1}{2}} + a_{{0}{2}} a_{{1}{0}}\\\\a_{{1}{0}} a_{{2}{1}} - a_{{1}{1}} a_{{2}{0}} & - a_{{0}{0}} a_{{2}{1}} + a_{{0}{1}} a_{{2}{0}} & a_{{0}{0}} a_{{1}{1}} - a_{{0}{1}} a_{{1}{0}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤  ⎡a_{{1}{1}}⋅a_{{2}{2}} - a_{{1}{2}}⋅a_{\n",
       "⎜⎢                                  ⎥  ⎢                                      \n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥, ⎢-a_{{1}{0}}⋅a_{{2}{2}} + a_{{1}{2}}⋅a_\n",
       "⎜⎢                                  ⎥  ⎢                                      \n",
       "⎝⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦  ⎣a_{{1}{0}}⋅a_{{2}{1}} - a_{{1}{1}}⋅a_{\n",
       "\n",
       "{2}{1}}   -a_{{0}{1}}⋅a_{{2}{2}} + a_{{0}{2}}⋅a_{{2}{1}}  a_{{0}{1}}⋅a_{{1}{2}\n",
       "                                                                              \n",
       "{{2}{0}}  a_{{0}{0}}⋅a_{{2}{2}} - a_{{0}{2}}⋅a_{{2}{0}}   -a_{{0}{0}}⋅a_{{1}{2\n",
       "                                                                              \n",
       "{2}{0}}   -a_{{0}{0}}⋅a_{{2}{1}} + a_{{0}{1}}⋅a_{{2}{0}}  a_{{0}{0}}⋅a_{{1}{1}\n",
       "\n",
       "} - a_{{0}{2}}⋅a_{{1}{1}} ⎤⎞\n",
       "                          ⎥⎟\n",
       "}} + a_{{0}{2}}⋅a_{{1}{0}}⎥⎟\n",
       "                          ⎥⎟\n",
       "} - a_{{0}{1}}⋅a_{{1}{0}} ⎦⎠"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa,xa.adjugate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa*xa.adjugate()-xa.adjugate()*xa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  0  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  0⎦"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa*xa.adjugate()-xa.det()*sp.eye(xn))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.7.共轭矩阵\n",
    "当$A=(a_{ij})$为复矩阵时,用$\\overline{a_{ij}}$表示$a_{ij}$的共轭复数,记$\\overline{A}=(\\overline{a_{ij}})$,$\\overline{A}称为A$的共轭矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**运算性质**(假设A、B为复矩阵,$\\lambda$为复数,且运算都是可行的):\n",
    "1. $\\overline{(A+B)}=\\overline A+\\overline B$\n",
    "2. $\\overline{\\lambda A}=\\bar \\lambda \\bar A$\n",
    "3. $\\overline{AB}=\\bar A \\bar B$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.逆矩阵\n",
    "矩阵并没有一个直接叫除法的操作。但有个与之相似的运算，叫做求逆运算。<br>\n",
    "对于$n$阶单位矩阵**$E$**以及同阶的方阵$A$,有$AE=EA=A$,从乘法的角度来看，$n$阶单位矩阵$E$在同阶方阵中的地位类似于1在复数中的地位.一个复数$a\\not =0$的倒数$a^{-1}$可以用等式$aa^{-1}=1$来刻画,类似的，**定义**:矩阵$A$的逆$A^{-1}$被定义为一个与$A$相乘后能得到一个单元矩阵的矩阵。即：$A\\times A^{-1}=E$。求逆这个操作本身是可逆的，一个矩阵的逆的逆也是这个矩阵本身。因此$A^{-1}\\times A=E$。根据这个特点我们可以推断出能求逆的矩阵，其行数和列数也必然相同。<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在方阵行列式一节，我们得到如下结论:$AA^*=A^*A=|A|E$.结合逆矩阵的性质:$A^{-1}\\times A=E$.$\\to$<br>\n",
    "**定理:**若$|A|\\not =0$,则方阵$A$可逆，而且$A^{-1}=\\frac{1}{|A|}A^*$.<br>\n",
    "**推论:**若$|A|\\not =0$,则$|A^{-1}|=\\frac{1}{|A|}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}}\\\\a_{{1}{0}} & a_{{1}{1}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}b_{{0}{0}} & b_{{0}{1}}\\\\b_{{1}{0}} & b_{{1}{1}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}⎤  ⎡b_{{0}{0}}  b_{{0}{1}}⎤⎞\n",
       "⎜⎢                      ⎥, ⎢                      ⎥⎟\n",
       "⎝⎣a_{{1}{0}}  a_{{1}{1}}⎦  ⎣b_{{1}{0}}  b_{{1}{1}}⎦⎠"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=2\n",
    "xa,xb=[getM(i,xn,xn) for i in 'ab']\n",
    "xa,xb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(1/xa.det()*xa.adjugate()-xa.inv())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAABHNCSVQICAgIfAhkiAAAAM5JREFUOI3N0rEyQ0EUxvEfJcMVWhoZ8Qpq75PnoDJ0Kl5BK0U67yAUiUYmpZGYoRDNuePOkXszYxS+5ps95/x3Z3c//lC7uMIz3jHCOVpNUBsTzHGDU/RjfY+dOvA2hrqpfhb1y0XQfjSHWE29DUwxw7o0cBzew2cCX3GHNRxl8DD8oeYaj+GdDBbhLzVgWd/K4DKthM8zWO5YWKzN6lwVHIR3asCD8B9v0Lb8O97Ed2T9KgDlqdXInfiO3EBD5GAP1xjjA0+4wHYT9I/1BRgnMa7SDNsAAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$0$$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa.inv().det()-1/xa.det())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**$|A|\\not =0 \\leftrightharpoons 方阵A可逆$,此时,称矩阵$A$为非奇异矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**如果$n$阶方阵$A、B$可逆,那么$A^{-1}、A^T、\\lambda A(\\lambda\\not=0)与AB$也可逆,则\n",
    "* ${(A^{-1})}^{-1}=A$,\n",
    "* ${(A^T)}^{-1}={(A^{-1})}^T$,\n",
    "* ${(\\lambda A)}^{-1}=\\frac 1 \\lambda A^{-1}$,\n",
    "* ${(AB)}^{-1}=B^{-1}A^{-1}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa.inv().inv()-xa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xa.T.inv()-xa.inv().T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify((x*xa).inv()-1/x*xa.inv())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify((xa*xb).inv()-xb.inv()*xa.inv())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线性变换$\\begin{cases}y_1=a_{11}x_1+\\dots+a_{1n}x_n\\\\\\vdots\\\\y_m=a_{m1}x_1+\\dots+a_{mn}x_n \\end{cases}$<br>\n",
    "的系数矩阵是一个$n$阶方阵$A$,若记$X=\\begin{bmatrix}x_1\\\\\\vdots\\\\x_n\\end{bmatrix},Y=\\begin{bmatrix}y_1\\\\\\vdots\\\\y_n\\end{bmatrix}$<br>\n",
    "则上述线性变换可记作$Y=AX$,反过来求求$变量y到x的线性变换相当于求方阵A$的逆矩阵$X=A^{-1}Y$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.矩阵分块法\n",
    "由于某些条件的限制,我们经常会遇到大型文件无法上传的情况,这时候可以使用数据分批处理的方法.\n",
    "\n",
    "用一些横线和竖线将矩阵分成若干个小块,这种操作称为**对矩阵进行分块**;<br>\n",
    "其中每一个小块称为**矩阵的子块**;<br>\n",
    "矩阵分块后，以子块为元素的形式上的矩阵称为**分块矩阵**.<br>\n",
    "\n",
    "对于行数和列数较高的的矩阵,运算时采用分块法,可以使大矩阵的运算化成小矩阵的运算,体现了**化整为零**的思想."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}} & a_{{0}{3}} & a_{{0}{4}} & a_{{0}{5}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}} & a_{{1}{3}} & a_{{1}{4}} & a_{{1}{5}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}} & a_{{2}{3}} & a_{{2}{4}} & a_{{2}{5}}\\\\a_{{3}{0}} & a_{{3}{1}} & a_{{3}{2}} & a_{{3}{3}} & a_{{3}{4}} & a_{{3}{5}}\\\\a_{{4}{0}} & a_{{4}{1}} & a_{{4}{2}} & a_{{4}{3}} & a_{{4}{4}} & a_{{4}{5}}\\\\a_{{5}{0}} & a_{{5}{1}} & a_{{5}{2}} & a_{{5}{3}} & a_{{5}{4}} & a_{{5}{5}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}b_{{0}{0}} & b_{{0}{1}} & b_{{0}{2}} & b_{{0}{3}} & b_{{0}{4}} & b_{{0}{5}}\\\\b_{{1}{0}} & b_{{1}{1}} & b_{{1}{2}} & b_{{1}{3}} & b_{{1}{4}} & b_{{1}{5}}\\\\b_{{2}{0}} & b_{{2}{1}} & b_{{2}{2}} & b_{{2}{3}} & b_{{2}{4}} & b_{{2}{5}}\\\\b_{{3}{0}} & b_{{3}{1}} & b_{{3}{2}} & b_{{3}{3}} & b_{{3}{4}} & b_{{3}{5}}\\\\b_{{4}{0}} & b_{{4}{1}} & b_{{4}{2}} & b_{{4}{3}} & b_{{4}{4}} & b_{{4}{5}}\\\\b_{{5}{0}} & b_{{5}{1}} & b_{{5}{2}} & b_{{5}{3}} & b_{{5}{4}} & b_{{5}{5}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}  a_{{0}{3}}  a_{{0}{4}}  a_{{0}{5}}⎤  ⎡b_\n",
       "⎜⎢                                                                      ⎥  ⎢  \n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}  a_{{1}{3}}  a_{{1}{4}}  a_{{1}{5}}⎥  ⎢b_\n",
       "⎜⎢                                                                      ⎥  ⎢  \n",
       "⎜⎢a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}  a_{{2}{3}}  a_{{2}{4}}  a_{{2}{5}}⎥  ⎢b_\n",
       "⎜⎢                                                                      ⎥, ⎢  \n",
       "⎜⎢a_{{3}{0}}  a_{{3}{1}}  a_{{3}{2}}  a_{{3}{3}}  a_{{3}{4}}  a_{{3}{5}}⎥  ⎢b_\n",
       "⎜⎢                                                                      ⎥  ⎢  \n",
       "⎜⎢a_{{4}{0}}  a_{{4}{1}}  a_{{4}{2}}  a_{{4}{3}}  a_{{4}{4}}  a_{{4}{5}}⎥  ⎢b_\n",
       "⎜⎢                                                                      ⎥  ⎢  \n",
       "⎝⎣a_{{5}{0}}  a_{{5}{1}}  a_{{5}{2}}  a_{{5}{3}}  a_{{5}{4}}  a_{{5}{5}}⎦  ⎣b_\n",
       "\n",
       "{{0}{0}}  b_{{0}{1}}  b_{{0}{2}}  b_{{0}{3}}  b_{{0}{4}}  b_{{0}{5}}⎤⎞\n",
       "                                                                    ⎥⎟\n",
       "{{1}{0}}  b_{{1}{1}}  b_{{1}{2}}  b_{{1}{3}}  b_{{1}{4}}  b_{{1}{5}}⎥⎟\n",
       "                                                                    ⎥⎟\n",
       "{{2}{0}}  b_{{2}{1}}  b_{{2}{2}}  b_{{2}{3}}  b_{{2}{4}}  b_{{2}{5}}⎥⎟\n",
       "                                                                    ⎥⎟\n",
       "{{3}{0}}  b_{{3}{1}}  b_{{3}{2}}  b_{{3}{3}}  b_{{3}{4}}  b_{{3}{5}}⎥⎟\n",
       "                                                                    ⎥⎟\n",
       "{{4}{0}}  b_{{4}{1}}  b_{{4}{2}}  b_{{4}{3}}  b_{{4}{4}}  b_{{4}{5}}⎥⎟\n",
       "                                                                    ⎥⎟\n",
       "{{5}{0}}  b_{{5}{1}}  b_{{5}{2}}  b_{{5}{3}}  b_{{5}{4}}  b_{{5}{5}}⎦⎠"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=6\n",
    "xa,xb=(getM(i,xn,xn) for i in 'ab')\n",
    "xa,xb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.1.分块矩阵的加法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} + b_{{0}{0}} & a_{{0}{1}} + b_{{0}{1}} & a_{{0}{2}} + b_{{0}{2}} & a_{{0}{3}} + b_{{0}{3}} & a_{{0}{4}} + b_{{0}{4}} & a_{{0}{5}} + b_{{0}{5}}\\\\a_{{1}{0}} + b_{{1}{0}} & a_{{1}{1}} + b_{{1}{1}} & a_{{1}{2}} + b_{{1}{2}} & a_{{1}{3}} + b_{{1}{3}} & a_{{1}{4}} + b_{{1}{4}} & a_{{1}{5}} + b_{{1}{5}}\\\\a_{{2}{0}} + b_{{2}{0}} & a_{{2}{1}} + b_{{2}{1}} & a_{{2}{2}} + b_{{2}{2}} & a_{{2}{3}} + b_{{2}{3}} & a_{{2}{4}} + b_{{2}{4}} & a_{{2}{5}} + b_{{2}{5}}\\\\a_{{3}{0}} + b_{{3}{0}} & a_{{3}{1}} + b_{{3}{1}} & a_{{3}{2}} + b_{{3}{2}} & a_{{3}{3}} + b_{{3}{3}} & a_{{3}{4}} + b_{{3}{4}} & a_{{3}{5}} + b_{{3}{5}}\\\\a_{{4}{0}} + b_{{4}{0}} & a_{{4}{1}} + b_{{4}{1}} & a_{{4}{2}} + b_{{4}{2}} & a_{{4}{3}} + b_{{4}{3}} & a_{{4}{4}} + b_{{4}{4}} & a_{{4}{5}} + b_{{4}{5}}\\\\a_{{5}{0}} + b_{{5}{0}} & a_{{5}{1}} + b_{{5}{1}} & a_{{5}{2}} + b_{{5}{2}} & a_{{5}{3}} + b_{{5}{3}} & a_{{5}{4}} + b_{{5}{4}} & a_{{5}{5}} + b_{{5}{5}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}} + b_{{0}{0}}  a_{{0}{1}} + b_{{0}{1}}  a_{{0}{2}} + b_{{0}{2}}  a_\n",
       "⎢                                                                             \n",
       "⎢a_{{1}{0}} + b_{{1}{0}}  a_{{1}{1}} + b_{{1}{1}}  a_{{1}{2}} + b_{{1}{2}}  a_\n",
       "⎢                                                                             \n",
       "⎢a_{{2}{0}} + b_{{2}{0}}  a_{{2}{1}} + b_{{2}{1}}  a_{{2}{2}} + b_{{2}{2}}  a_\n",
       "⎢                                                                             \n",
       "⎢a_{{3}{0}} + b_{{3}{0}}  a_{{3}{1}} + b_{{3}{1}}  a_{{3}{2}} + b_{{3}{2}}  a_\n",
       "⎢                                                                             \n",
       "⎢a_{{4}{0}} + b_{{4}{0}}  a_{{4}{1}} + b_{{4}{1}}  a_{{4}{2}} + b_{{4}{2}}  a_\n",
       "⎢                                                                             \n",
       "⎣a_{{5}{0}} + b_{{5}{0}}  a_{{5}{1}} + b_{{5}{1}}  a_{{5}{2}} + b_{{5}{2}}  a_\n",
       "\n",
       "{{0}{3}} + b_{{0}{3}}  a_{{0}{4}} + b_{{0}{4}}  a_{{0}{5}} + b_{{0}{5}}⎤\n",
       "                                                                       ⎥\n",
       "{{1}{3}} + b_{{1}{3}}  a_{{1}{4}} + b_{{1}{4}}  a_{{1}{5}} + b_{{1}{5}}⎥\n",
       "                                                                       ⎥\n",
       "{{2}{3}} + b_{{2}{3}}  a_{{2}{4}} + b_{{2}{4}}  a_{{2}{5}} + b_{{2}{5}}⎥\n",
       "                                                                       ⎥\n",
       "{{3}{3}} + b_{{3}{3}}  a_{{3}{4}} + b_{{3}{4}}  a_{{3}{5}} + b_{{3}{5}}⎥\n",
       "                                                                       ⎥\n",
       "{{4}{3}} + b_{{4}{3}}  a_{{4}{4}} + b_{{4}{4}}  a_{{4}{5}} + b_{{4}{5}}⎥\n",
       "                                                                       ⎥\n",
       "{{5}{3}} + b_{{5}{3}}  a_{{5}{4}} + b_{{5}{4}}  a_{{5}{5}} + b_{{5}{5}}⎦"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xm=xn//2\n",
    "sp.Matrix.row_join(xa[:,:xm]+xb[:,:xm],xa[:,xm:]+xb[:,xm:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.2.分块矩阵的数乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} x & a_{{0}{1}} x & a_{{0}{2}} x & a_{{0}{0}} x & a_{{0}{1}} x & a_{{0}{2}} x\\\\a_{{1}{0}} x & a_{{1}{1}} x & a_{{1}{2}} x & a_{{1}{0}} x & a_{{1}{1}} x & a_{{1}{2}} x\\\\a_{{2}{0}} x & a_{{2}{1}} x & a_{{2}{2}} x & a_{{2}{0}} x & a_{{2}{1}} x & a_{{2}{2}} x\\\\a_{{3}{0}} x & a_{{3}{1}} x & a_{{3}{2}} x & a_{{3}{0}} x & a_{{3}{1}} x & a_{{3}{2}} x\\\\a_{{4}{0}} x & a_{{4}{1}} x & a_{{4}{2}} x & a_{{4}{0}} x & a_{{4}{1}} x & a_{{4}{2}} x\\\\a_{{5}{0}} x & a_{{5}{1}} x & a_{{5}{2}} x & a_{{5}{0}} x & a_{{5}{1}} x & a_{{5}{2}} x\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}⋅x  a_{{0}{1}}⋅x  a_{{0}{2}}⋅x  a_{{0}{0}}⋅x  a_{{0}{1}}⋅x  a_{{0}{\n",
       "⎢                                                                             \n",
       "⎢a_{{1}{0}}⋅x  a_{{1}{1}}⋅x  a_{{1}{2}}⋅x  a_{{1}{0}}⋅x  a_{{1}{1}}⋅x  a_{{1}{\n",
       "⎢                                                                             \n",
       "⎢a_{{2}{0}}⋅x  a_{{2}{1}}⋅x  a_{{2}{2}}⋅x  a_{{2}{0}}⋅x  a_{{2}{1}}⋅x  a_{{2}{\n",
       "⎢                                                                             \n",
       "⎢a_{{3}{0}}⋅x  a_{{3}{1}}⋅x  a_{{3}{2}}⋅x  a_{{3}{0}}⋅x  a_{{3}{1}}⋅x  a_{{3}{\n",
       "⎢                                                                             \n",
       "⎢a_{{4}{0}}⋅x  a_{{4}{1}}⋅x  a_{{4}{2}}⋅x  a_{{4}{0}}⋅x  a_{{4}{1}}⋅x  a_{{4}{\n",
       "⎢                                                                             \n",
       "⎣a_{{5}{0}}⋅x  a_{{5}{1}}⋅x  a_{{5}{2}}⋅x  a_{{5}{0}}⋅x  a_{{5}{1}}⋅x  a_{{5}{\n",
       "\n",
       "2}}⋅x⎤\n",
       "     ⎥\n",
       "2}}⋅x⎥\n",
       "     ⎥\n",
       "2}}⋅x⎥\n",
       "     ⎥\n",
       "2}}⋅x⎥\n",
       "     ⎥\n",
       "2}}⋅x⎥\n",
       "     ⎥\n",
       "2}}⋅x⎦"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Matrix.row_join(xa[:,:xm]*x,xa[:,:xm]*x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.3.分块矩阵的乘法\n",
    "设$A\\in\\mathbb R^{m\\times l},B\\in\\mathbb{R}^{l\\times n}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}} & a_{{0}{3}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}} & a_{{1}{3}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{0}} & a_{{3}{1}} & a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}b_{{0}{0}} & b_{{0}{1}} & b_{{0}{2}} & b_{{0}{3}}\\\\b_{{1}{0}} & b_{{1}{1}} & b_{{1}{2}} & b_{{1}{3}}\\\\b_{{2}{0}} & b_{{2}{1}} & b_{{2}{2}} & b_{{2}{3}}\\\\b_{{3}{0}} & b_{{3}{1}} & b_{{3}{2}} & b_{{3}{3}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}  a_{{0}{3}}⎤  ⎡b_{{0}{0}}  b_{{0}{1}}  b_\n",
       "⎜⎢                                              ⎥  ⎢                          \n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}  a_{{1}{3}}⎥  ⎢b_{{1}{0}}  b_{{1}{1}}  b_\n",
       "⎜⎢                                              ⎥, ⎢                          \n",
       "⎜⎢a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}  a_{{2}{3}}⎥  ⎢b_{{2}{0}}  b_{{2}{1}}  b_\n",
       "⎜⎢                                              ⎥  ⎢                          \n",
       "⎝⎣a_{{3}{0}}  a_{{3}{1}}  a_{{3}{2}}  a_{{3}{3}}⎦  ⎣b_{{3}{0}}  b_{{3}{1}}  b_\n",
       "\n",
       "{{0}{2}}  b_{{0}{3}}⎤⎞\n",
       "                    ⎥⎟\n",
       "{{1}{2}}  b_{{1}{3}}⎥⎟\n",
       "                    ⎥⎟\n",
       "{{2}{2}}  b_{{2}{3}}⎥⎟\n",
       "                    ⎥⎟\n",
       "{{3}{2}}  b_{{3}{3}}⎦⎠"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=4\n",
    "xm=xn//2\n",
    "xa,xb=(getM(i,xn,xn) for i in 'ab')\n",
    "xa,xb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left [ \\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}}\\\\a_{{1}{0}} & a_{{1}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{2}} & a_{{0}{3}}\\\\a_{{1}{2}} & a_{{1}{3}}\\end{matrix}\\right]\\\\\\left[\\begin{matrix}a_{{2}{0}} & a_{{2}{1}}\\\\a_{{3}{0}} & a_{{3}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right]\\end{matrix}\\right], \\quad \\left[\\begin{matrix}\\left[\\begin{matrix}b_{{0}{0}} & b_{{0}{1}}\\\\b_{{1}{0}} & b_{{1}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}b_{{0}{2}} & b_{{0}{3}}\\\\b_{{1}{2}} & b_{{1}{3}}\\end{matrix}\\right]\\\\\\left[\\begin{matrix}b_{{2}{0}} & b_{{2}{1}}\\\\b_{{3}{0}} & b_{{3}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}b_{{2}{2}} & b_{{2}{3}}\\\\b_{{3}{2}} & b_{{3}{3}}\\end{matrix}\\right]\\end{matrix}\\right]\\right ]$$"
      ],
      "text/plain": [
       "⎡⎡⎡a_{{0}{0}}  a_{{0}{1}}⎤  ⎡a_{{0}{2}}  a_{{0}{3}}⎤⎤  ⎡⎡b_{{0}{0}}  b_{{0}{1}\n",
       "⎢⎢⎢                      ⎥  ⎢                      ⎥⎥  ⎢⎢                     \n",
       "⎢⎢⎣a_{{1}{0}}  a_{{1}{1}}⎦  ⎣a_{{1}{2}}  a_{{1}{3}}⎦⎥  ⎢⎣b_{{1}{0}}  b_{{1}{1}\n",
       "⎢⎢                                                  ⎥, ⎢                      \n",
       "⎢⎢⎡a_{{2}{0}}  a_{{2}{1}}⎤  ⎡a_{{2}{2}}  a_{{2}{3}}⎤⎥  ⎢⎡b_{{2}{0}}  b_{{2}{1}\n",
       "⎢⎢⎢                      ⎥  ⎢                      ⎥⎥  ⎢⎢                     \n",
       "⎣⎣⎣a_{{3}{0}}  a_{{3}{1}}⎦  ⎣a_{{3}{2}}  a_{{3}{3}}⎦⎦  ⎣⎣b_{{3}{0}}  b_{{3}{1}\n",
       "\n",
       "}⎤  ⎡b_{{0}{2}}  b_{{0}{3}}⎤⎤⎤\n",
       " ⎥  ⎢                      ⎥⎥⎥\n",
       "}⎦  ⎣b_{{1}{2}}  b_{{1}{3}}⎦⎥⎥\n",
       "                            ⎥⎥\n",
       "}⎤  ⎡b_{{2}{2}}  b_{{2}{3}}⎤⎥⎥\n",
       " ⎥  ⎢                      ⎥⎥⎥\n",
       "}⎦  ⎣b_{{3}{2}}  b_{{3}{3}}⎦⎦⎦"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs=[sp.Matrix(((i[:xm,:xm],i[:xm,xm:]),(i[xm:,:xm],i[xm:,xm:]))) for i in [xa,xb]]\n",
    "xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} b_{{0}{0}} + a_{{0}{1}} b_{{1}{0}} + a_{{0}{2}} b_{{2}{0}} + a_{{0}{3}} b_{{3}{0}} & a_{{0}{0}} b_{{0}{1}} + a_{{0}{1}} b_{{1}{1}} + a_{{0}{2}} b_{{2}{1}} + a_{{0}{3}} b_{{3}{1}}\\\\a_{{1}{0}} b_{{0}{0}} + a_{{1}{1}} b_{{1}{0}} + a_{{1}{2}} b_{{2}{0}} + a_{{1}{3}} b_{{3}{0}} & a_{{1}{0}} b_{{0}{1}} + a_{{1}{1}} b_{{1}{1}} + a_{{1}{2}} b_{{2}{1}} + a_{{1}{3}} b_{{3}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{0}} b_{{0}{2}} + a_{{0}{1}} b_{{1}{2}} + a_{{0}{2}} b_{{2}{2}} + a_{{0}{3}} b_{{3}{2}} & a_{{0}{0}} b_{{0}{3}} + a_{{0}{1}} b_{{1}{3}} + a_{{0}{2}} b_{{2}{3}} + a_{{0}{3}} b_{{3}{3}}\\\\a_{{1}{0}} b_{{0}{2}} + a_{{1}{1}} b_{{1}{2}} + a_{{1}{2}} b_{{2}{2}} + a_{{1}{3}} b_{{3}{2}} & a_{{1}{0}} b_{{0}{3}} + a_{{1}{1}} b_{{1}{3}} + a_{{1}{2}} b_{{2}{3}} + a_{{1}{3}} b_{{3}{3}}\\end{matrix}\\right]\\\\\\left[\\begin{matrix}a_{{2}{0}} b_{{0}{0}} + a_{{2}{1}} b_{{1}{0}} + a_{{2}{2}} b_{{2}{0}} + a_{{2}{3}} b_{{3}{0}} & a_{{2}{0}} b_{{0}{1}} + a_{{2}{1}} b_{{1}{1}} + a_{{2}{2}} b_{{2}{1}} + a_{{2}{3}} b_{{3}{1}}\\\\a_{{3}{0}} b_{{0}{0}} + a_{{3}{1}} b_{{1}{0}} + a_{{3}{2}} b_{{2}{0}} + a_{{3}{3}} b_{{3}{0}} & a_{{3}{0}} b_{{0}{1}} + a_{{3}{1}} b_{{1}{1}} + a_{{3}{2}} b_{{2}{1}} + a_{{3}{3}} b_{{3}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{2}{0}} b_{{0}{2}} + a_{{2}{1}} b_{{1}{2}} + a_{{2}{2}} b_{{2}{2}} + a_{{2}{3}} b_{{3}{2}} & a_{{2}{0}} b_{{0}{3}} + a_{{2}{1}} b_{{1}{3}} + a_{{2}{2}} b_{{2}{3}} + a_{{2}{3}} b_{{3}{3}}\\\\a_{{3}{0}} b_{{0}{2}} + a_{{3}{1}} b_{{1}{2}} + a_{{3}{2}} b_{{2}{2}} + a_{{3}{3}} b_{{3}{2}} & a_{{3}{0}} b_{{0}{3}} + a_{{3}{1}} b_{{1}{3}} + a_{{3}{2}} b_{{2}{3}} + a_{{3}{3}} b_{{3}{3}}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}⋅b_{{0}{0}} + a_{{0}{1}}⋅b_{{1}{0}} + a_{{0}{2}}⋅b_{{2}{0}} + a_{{\n",
       "⎢⎢                                                                            \n",
       "⎢⎣a_{{1}{0}}⋅b_{{0}{0}} + a_{{1}{1}}⋅b_{{1}{0}} + a_{{1}{2}}⋅b_{{2}{0}} + a_{{\n",
       "⎢                                                                             \n",
       "⎢⎡a_{{2}{0}}⋅b_{{0}{0}} + a_{{2}{1}}⋅b_{{1}{0}} + a_{{2}{2}}⋅b_{{2}{0}} + a_{{\n",
       "⎢⎢                                                                            \n",
       "⎣⎣a_{{3}{0}}⋅b_{{0}{0}} + a_{{3}{1}}⋅b_{{1}{0}} + a_{{3}{2}}⋅b_{{2}{0}} + a_{{\n",
       "\n",
       "0}{3}}⋅b_{{3}{0}}  a_{{0}{0}}⋅b_{{0}{1}} + a_{{0}{1}}⋅b_{{1}{1}} + a_{{0}{2}}⋅\n",
       "                                                                              \n",
       "1}{3}}⋅b_{{3}{0}}  a_{{1}{0}}⋅b_{{0}{1}} + a_{{1}{1}}⋅b_{{1}{1}} + a_{{1}{2}}⋅\n",
       "                                                                              \n",
       "2}{3}}⋅b_{{3}{0}}  a_{{2}{0}}⋅b_{{0}{1}} + a_{{2}{1}}⋅b_{{1}{1}} + a_{{2}{2}}⋅\n",
       "                                                                              \n",
       "3}{3}}⋅b_{{3}{0}}  a_{{3}{0}}⋅b_{{0}{1}} + a_{{3}{1}}⋅b_{{1}{1}} + a_{{3}{2}}⋅\n",
       "\n",
       "b_{{2}{1}} + a_{{0}{3}}⋅b_{{3}{1}}⎤  ⎡a_{{0}{0}}⋅b_{{0}{2}} + a_{{0}{1}}⋅b_{{1\n",
       "                                  ⎥  ⎢                                        \n",
       "b_{{2}{1}} + a_{{1}{3}}⋅b_{{3}{1}}⎦  ⎣a_{{1}{0}}⋅b_{{0}{2}} + a_{{1}{1}}⋅b_{{1\n",
       "                                                                              \n",
       "b_{{2}{1}} + a_{{2}{3}}⋅b_{{3}{1}}⎤  ⎡a_{{2}{0}}⋅b_{{0}{2}} + a_{{2}{1}}⋅b_{{1\n",
       "                                  ⎥  ⎢                                        \n",
       "b_{{2}{1}} + a_{{3}{3}}⋅b_{{3}{1}}⎦  ⎣a_{{3}{0}}⋅b_{{0}{2}} + a_{{3}{1}}⋅b_{{1\n",
       "\n",
       "}{2}} + a_{{0}{2}}⋅b_{{2}{2}} + a_{{0}{3}}⋅b_{{3}{2}}  a_{{0}{0}}⋅b_{{0}{3}} +\n",
       "                                                                              \n",
       "}{2}} + a_{{1}{2}}⋅b_{{2}{2}} + a_{{1}{3}}⋅b_{{3}{2}}  a_{{1}{0}}⋅b_{{0}{3}} +\n",
       "                                                                              \n",
       "}{2}} + a_{{2}{2}}⋅b_{{2}{2}} + a_{{2}{3}}⋅b_{{3}{2}}  a_{{2}{0}}⋅b_{{0}{3}} +\n",
       "                                                                              \n",
       "}{2}} + a_{{3}{2}}⋅b_{{2}{2}} + a_{{3}{3}}⋅b_{{3}{2}}  a_{{3}{0}}⋅b_{{0}{3}} +\n",
       "\n",
       " a_{{0}{1}}⋅b_{{1}{3}} + a_{{0}{2}}⋅b_{{2}{3}} + a_{{0}{3}}⋅b_{{3}{3}}⎤⎤\n",
       "                                                                      ⎥⎥\n",
       " a_{{1}{1}}⋅b_{{1}{3}} + a_{{1}{2}}⋅b_{{2}{3}} + a_{{1}{3}}⋅b_{{3}{3}}⎦⎥\n",
       "                                                                       ⎥\n",
       " a_{{2}{1}}⋅b_{{1}{3}} + a_{{2}{2}}⋅b_{{2}{3}} + a_{{2}{3}}⋅b_{{3}{3}}⎤⎥\n",
       "                                                                      ⎥⎥\n",
       " a_{{3}{1}}⋅b_{{1}{3}} + a_{{3}{2}}⋅b_{{2}{3}} + a_{{3}{3}}⋅b_{{3}{3}}⎦⎦"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs=xs[0]*xs[1]\n",
    "xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} b_{{0}{0}} + a_{{0}{1}} b_{{1}{0}} + a_{{0}{2}} b_{{2}{0}} + a_{{0}{3}} b_{{3}{0}} & a_{{0}{0}} b_{{0}{1}} + a_{{0}{1}} b_{{1}{1}} + a_{{0}{2}} b_{{2}{1}} + a_{{0}{3}} b_{{3}{1}} & a_{{0}{0}} b_{{0}{2}} + a_{{0}{1}} b_{{1}{2}} + a_{{0}{2}} b_{{2}{2}} + a_{{0}{3}} b_{{3}{2}} & a_{{0}{0}} b_{{0}{3}} + a_{{0}{1}} b_{{1}{3}} + a_{{0}{2}} b_{{2}{3}} + a_{{0}{3}} b_{{3}{3}}\\\\a_{{1}{0}} b_{{0}{0}} + a_{{1}{1}} b_{{1}{0}} + a_{{1}{2}} b_{{2}{0}} + a_{{1}{3}} b_{{3}{0}} & a_{{1}{0}} b_{{0}{1}} + a_{{1}{1}} b_{{1}{1}} + a_{{1}{2}} b_{{2}{1}} + a_{{1}{3}} b_{{3}{1}} & a_{{1}{0}} b_{{0}{2}} + a_{{1}{1}} b_{{1}{2}} + a_{{1}{2}} b_{{2}{2}} + a_{{1}{3}} b_{{3}{2}} & a_{{1}{0}} b_{{0}{3}} + a_{{1}{1}} b_{{1}{3}} + a_{{1}{2}} b_{{2}{3}} + a_{{1}{3}} b_{{3}{3}}\\\\a_{{2}{0}} b_{{0}{0}} + a_{{2}{1}} b_{{1}{0}} + a_{{2}{2}} b_{{2}{0}} + a_{{2}{3}} b_{{3}{0}} & a_{{2}{0}} b_{{0}{1}} + a_{{2}{1}} b_{{1}{1}} + a_{{2}{2}} b_{{2}{1}} + a_{{2}{3}} b_{{3}{1}} & a_{{2}{0}} b_{{0}{2}} + a_{{2}{1}} b_{{1}{2}} + a_{{2}{2}} b_{{2}{2}} + a_{{2}{3}} b_{{3}{2}} & a_{{2}{0}} b_{{0}{3}} + a_{{2}{1}} b_{{1}{3}} + a_{{2}{2}} b_{{2}{3}} + a_{{2}{3}} b_{{3}{3}}\\\\a_{{3}{0}} b_{{0}{0}} + a_{{3}{1}} b_{{1}{0}} + a_{{3}{2}} b_{{2}{0}} + a_{{3}{3}} b_{{3}{0}} & a_{{3}{0}} b_{{0}{1}} + a_{{3}{1}} b_{{1}{1}} + a_{{3}{2}} b_{{2}{1}} + a_{{3}{3}} b_{{3}{1}} & a_{{3}{0}} b_{{0}{2}} + a_{{3}{1}} b_{{1}{2}} + a_{{3}{2}} b_{{2}{2}} + a_{{3}{3}} b_{{3}{2}} & a_{{3}{0}} b_{{0}{3}} + a_{{3}{1}} b_{{1}{3}} + a_{{3}{2}} b_{{2}{3}} + a_{{3}{3}} b_{{3}{3}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}⋅b_{{0}{0}} + a_{{0}{1}}⋅b_{{1}{0}} + a_{{0}{2}}⋅b_{{2}{0}} + a_{{0\n",
       "⎢                                                                             \n",
       "⎢a_{{1}{0}}⋅b_{{0}{0}} + a_{{1}{1}}⋅b_{{1}{0}} + a_{{1}{2}}⋅b_{{2}{0}} + a_{{1\n",
       "⎢                                                                             \n",
       "⎢a_{{2}{0}}⋅b_{{0}{0}} + a_{{2}{1}}⋅b_{{1}{0}} + a_{{2}{2}}⋅b_{{2}{0}} + a_{{2\n",
       "⎢                                                                             \n",
       "⎣a_{{3}{0}}⋅b_{{0}{0}} + a_{{3}{1}}⋅b_{{1}{0}} + a_{{3}{2}}⋅b_{{2}{0}} + a_{{3\n",
       "\n",
       "}{3}}⋅b_{{3}{0}}  a_{{0}{0}}⋅b_{{0}{1}} + a_{{0}{1}}⋅b_{{1}{1}} + a_{{0}{2}}⋅b\n",
       "                                                                              \n",
       "}{3}}⋅b_{{3}{0}}  a_{{1}{0}}⋅b_{{0}{1}} + a_{{1}{1}}⋅b_{{1}{1}} + a_{{1}{2}}⋅b\n",
       "                                                                              \n",
       "}{3}}⋅b_{{3}{0}}  a_{{2}{0}}⋅b_{{0}{1}} + a_{{2}{1}}⋅b_{{1}{1}} + a_{{2}{2}}⋅b\n",
       "                                                                              \n",
       "}{3}}⋅b_{{3}{0}}  a_{{3}{0}}⋅b_{{0}{1}} + a_{{3}{1}}⋅b_{{1}{1}} + a_{{3}{2}}⋅b\n",
       "\n",
       "_{{2}{1}} + a_{{0}{3}}⋅b_{{3}{1}}  a_{{0}{0}}⋅b_{{0}{2}} + a_{{0}{1}}⋅b_{{1}{2\n",
       "                                                                              \n",
       "_{{2}{1}} + a_{{1}{3}}⋅b_{{3}{1}}  a_{{1}{0}}⋅b_{{0}{2}} + a_{{1}{1}}⋅b_{{1}{2\n",
       "                                                                              \n",
       "_{{2}{1}} + a_{{2}{3}}⋅b_{{3}{1}}  a_{{2}{0}}⋅b_{{0}{2}} + a_{{2}{1}}⋅b_{{1}{2\n",
       "                                                                              \n",
       "_{{2}{1}} + a_{{3}{3}}⋅b_{{3}{1}}  a_{{3}{0}}⋅b_{{0}{2}} + a_{{3}{1}}⋅b_{{1}{2\n",
       "\n",
       "}} + a_{{0}{2}}⋅b_{{2}{2}} + a_{{0}{3}}⋅b_{{3}{2}}  a_{{0}{0}}⋅b_{{0}{3}} + a_\n",
       "                                                                              \n",
       "}} + a_{{1}{2}}⋅b_{{2}{2}} + a_{{1}{3}}⋅b_{{3}{2}}  a_{{1}{0}}⋅b_{{0}{3}} + a_\n",
       "                                                                              \n",
       "}} + a_{{2}{2}}⋅b_{{2}{2}} + a_{{2}{3}}⋅b_{{3}{2}}  a_{{2}{0}}⋅b_{{0}{3}} + a_\n",
       "                                                                              \n",
       "}} + a_{{3}{2}}⋅b_{{2}{2}} + a_{{3}{3}}⋅b_{{3}{2}}  a_{{3}{0}}⋅b_{{0}{3}} + a_\n",
       "\n",
       "{{0}{1}}⋅b_{{1}{3}} + a_{{0}{2}}⋅b_{{2}{3}} + a_{{0}{3}}⋅b_{{3}{3}}⎤\n",
       "                                                                   ⎥\n",
       "{{1}{1}}⋅b_{{1}{3}} + a_{{1}{2}}⋅b_{{2}{3}} + a_{{1}{3}}⋅b_{{3}{3}}⎥\n",
       "                                                                   ⎥\n",
       "{{2}{1}}⋅b_{{1}{3}} + a_{{2}{2}}⋅b_{{2}{3}} + a_{{2}{3}}⋅b_{{3}{3}}⎥\n",
       "                                                                   ⎥\n",
       "{{3}{1}}⋅b_{{1}{3}} + a_{{3}{2}}⋅b_{{2}{3}} + a_{{3}{3}}⋅b_{{3}{3}}⎦"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs=sp.Matrix.row_join(*[sp.Matrix.col_join(*[xs[i,j] for i in range(xm)]) for j in range(xm)])\n",
    "xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0  0  0⎤\n",
       "⎢          ⎥\n",
       "⎢0  0  0  0⎥\n",
       "⎢          ⎥\n",
       "⎢0  0  0  0⎥\n",
       "⎢          ⎥\n",
       "⎣0  0  0  0⎦"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs-xa*xb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.4.分块矩阵的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}}\\\\a_{{1}{0}} & a_{{1}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{2}} & a_{{0}{3}}\\\\a_{{1}{2}} & a_{{1}{3}}\\end{matrix}\\right]\\\\\\left[\\begin{matrix}a_{{2}{0}} & a_{{2}{1}}\\\\a_{{3}{0}} & a_{{3}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}  a_{{0}{1}}⎤  ⎡a_{{0}{2}}  a_{{0}{3}}⎤⎤\n",
       "⎢⎢                      ⎥  ⎢                      ⎥⎥\n",
       "⎢⎣a_{{1}{0}}  a_{{1}{1}}⎦  ⎣a_{{1}{2}}  a_{{1}{3}}⎦⎥\n",
       "⎢                                                  ⎥\n",
       "⎢⎡a_{{2}{0}}  a_{{2}{1}}⎤  ⎡a_{{2}{2}}  a_{{2}{3}}⎤⎥\n",
       "⎢⎢                      ⎥  ⎢                      ⎥⎥\n",
       "⎣⎣a_{{3}{0}}  a_{{3}{1}}⎦  ⎣a_{{3}{2}}  a_{{3}{3}}⎦⎦"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs=sp.Matrix(((xa[:xm,:xm],xa[:xm,xm:]),(xa[xm:,:xm],xa[xm:,xm:])))\n",
    "xs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分块矩阵不仅形式上的大矩阵进行转置，而且每一个子块小矩阵也进行转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} & a_{{1}{0}} & a_{{2}{0}} & a_{{3}{0}}\\\\a_{{0}{1}} & a_{{1}{1}} & a_{{2}{1}} & a_{{3}{1}}\\\\a_{{0}{2}} & a_{{1}{2}} & a_{{2}{2}} & a_{{3}{2}}\\\\a_{{0}{3}} & a_{{1}{3}} & a_{{2}{3}} & a_{{3}{3}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}  a_{{1}{0}}  a_{{2}{0}}  a_{{3}{0}}⎤\n",
       "⎢                                              ⎥\n",
       "⎢a_{{0}{1}}  a_{{1}{1}}  a_{{2}{1}}  a_{{3}{1}}⎥\n",
       "⎢                                              ⎥\n",
       "⎢a_{{0}{2}}  a_{{1}{2}}  a_{{2}{2}}  a_{{3}{2}}⎥\n",
       "⎢                                              ⎥\n",
       "⎣a_{{0}{3}}  a_{{1}{3}}  a_{{2}{3}}  a_{{3}{3}}⎦"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs=xs.T\n",
    "xs=sp.Matrix.row_join(*[sp.Matrix.col_join(*[xs[i,j].T for i in range(xm)]) for j in range(xm)])\n",
    "xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa.T==xs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.5.分块对角矩阵\n",
    "**定义:**设$A$是$n$阶矩阵，若：\n",
    "1. $A$的分块矩阵只有在对角线上有非零子块,\n",
    "2. 其余子块都为零矩阵,\n",
    "3. 对角线上的子块都是方阵,\n",
    "\n",
    "那么称$A$为**分块对角矩阵**.如:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left [ \\left[\\begin{matrix}a_{0}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}b_{0}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}c_{{0}{0}} & c_{{0}{1}}\\\\c_{{1}{0}} & c_{{1}{1}}\\end{matrix}\\right]\\right ], \\quad \\left[\\begin{matrix}a_{0} & 0 & 0 & 0\\\\0 & b_{0} & 0 & 0\\\\0 & 0 & c_{{0}{0}} & c_{{0}{1}}\\\\0 & 0 & c_{{1}{0}} & c_{{1}{1}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛                                              ⎡a_{0}    0        0           \n",
       "⎜                                              ⎢                              \n",
       "⎜⎡                  ⎡c_{{0}{0}}  c_{{0}{1}}⎤⎤  ⎢  0    b_{0}      0           \n",
       "⎜⎢[a_{0}], [b_{0}], ⎢                      ⎥⎥, ⎢                              \n",
       "⎜⎣                  ⎣c_{{1}{0}}  c_{{1}{1}}⎦⎦  ⎢  0      0    c_{{0}{0}}  c_{{\n",
       "⎜                                              ⎢                              \n",
       "⎝                                              ⎣  0      0    c_{{1}{0}}  c_{{\n",
       "\n",
       "0     ⎤⎞\n",
       "      ⎥⎟\n",
       "0     ⎥⎟\n",
       "      ⎥⎟\n",
       "0}{1}}⎥⎟\n",
       "      ⎥⎟\n",
       "1}{1}}⎦⎠"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=[getM(i,j,k) for i,j,k in (('a',1,1),('b',1,1),('c',2,2))]\n",
    "xs=sp.diag(*xa)\n",
    "xa,xs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**分块对角阵的性质**:\n",
    "* $|A|=|A_1|\\dots|A_s|$\n",
    "* 若$|A_i|\\not =0,则|A|\\not=0$,并且矩阵$A$的逆等于分块矩阵各子块取逆之后的新矩阵。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABoAAAASCAYAAABFGc6jAAAABHNCSVQICAgIfAhkiAAAARZJREFUOI3t1L0uRFEQwPEfkZBsp1IhEYlERK2xV5QaBZ14CsUWCg+gkFBQUUiUOgqFhDdQqdYWCkHhY1Uiq7hnZV337t7s2mj8k5OZTGbOzOTMGf6IJWzjEi+o4bCJfyX4pJ27Rse+ROA6plHFLSZyFPeMrRR7tVnQHMbRg0i+jio5ivnR0XmeoHZIJmqHfqxgGG+4wgU+8l4QaX8Yyig2OvbmzZrBPuYxhAKmsIdRnIoHqyWR1h1lsRlij+uGTjvKYjfI2W4nug+y0O1EM0GWfyPRJAZT7CPYCfrX+yb/0WI4xJNUr+4g6I9YC/oySuJPfoNXjGEBAzgRD0UqG7KXZM33dVPEEa7xhHc84Ayr4jX2T+d8AureRFnMrt8+AAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$15$$"
      ],
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reduce(lambda x,y:x+y,range(6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/latex": [
       "$$\\left ( a_{0} b_{0} c_{{0}{0}} c_{{1}{1}} - a_{0} b_{0} c_{{0}{1}} c_{{1}{0}}, \\quad a_{0} b_{0} \\left(c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}\\right)\\right )$$"
      ],
      "text/plain": [
       "(a_{0}⋅b_{0}⋅c_{{0}{0}}⋅c_{{1}{1}} - a_{0}⋅b_{0}⋅c_{{0}{1}}⋅c_{{1}{0}}, a_{0}⋅\n",
       "b_{0}⋅(c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}))"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs.det(),reduce(lambda i,j:i*j,[i.det() for i in xa])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAABHNCSVQICAgIfAhkiAAAAM5JREFUOI3N0rEyQ0EUxvEfJcMVWhoZ8Qpq75PnoDJ0Kl5BK0U67yAUiUYmpZGYoRDNuePOkXszYxS+5ps95/x3Z3c//lC7uMIz3jHCOVpNUBsTzHGDU/RjfY+dOvA2hrqpfhb1y0XQfjSHWE29DUwxw7o0cBzew2cCX3GHNRxl8DD8oeYaj+GdDBbhLzVgWd/K4DKthM8zWO5YWKzN6lwVHIR3asCD8B9v0Lb8O97Ed2T9KgDlqdXInfiO3EBD5GAP1xjjA0+4wHYT9I/1BRgnMa7SDNsAAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$0$$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.simplify(xs.det()-reduce(lambda i,j:i*j,[i.det() for i in xa]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}\\frac{1}{a_{0}} & 0 & 0 & 0\\\\0 & \\frac{1}{b_{0}} & 0 & 0\\\\0 & 0 & \\frac{c_{{1}{1}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}} & - \\frac{c_{{0}{1}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}}\\\\0 & 0 & - \\frac{c_{{1}{0}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}} & \\frac{c_{{0}{0}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}\\frac{1}{a_{0}} & 0 & 0 & 0\\\\0 & \\frac{1}{b_{0}} & 0 & 0\\\\0 & 0 & \\frac{c_{{1}{1}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}} & - \\frac{c_{{0}{1}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}}\\\\0 & 0 & - \\frac{c_{{1}{0}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}} & \\frac{c_{{0}{0}}}{c_{{0}{0}} c_{{1}{1}} - c_{{0}{1}} c_{{1}{0}}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡  1                                                                         \n",
       "⎜⎢─────    0                          0                                       \n",
       "⎜⎢a_{0}                                                                       \n",
       "⎜⎢                                                                            \n",
       "⎜⎢         1                                                                  \n",
       "⎜⎢  0    ─────                        0                                       \n",
       "⎜⎢       b_{0}                                                                \n",
       "⎜⎢                                                                            \n",
       "⎜⎢                                c_{{1}{1}}                                  \n",
       "⎜⎢  0      0    ─────────────────────────────────────────────  ───────────────\n",
       "⎜⎢              c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}  c_{{0}{0}}⋅c_{{\n",
       "⎜⎢                                                                            \n",
       "⎜⎢                               -c_{{1}{0}}                                  \n",
       "⎜⎢  0      0    ─────────────────────────────────────────────  ───────────────\n",
       "⎝⎣              c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}  c_{{0}{0}}⋅c_{{\n",
       "\n",
       "                              ⎤  ⎡  1                                         \n",
       "       0                      ⎥  ⎢─────    0                          0       \n",
       "                              ⎥  ⎢a_{0}                                       \n",
       "                              ⎥  ⎢                                            \n",
       "                              ⎥  ⎢         1                                  \n",
       "       0                      ⎥  ⎢  0    ─────                        0       \n",
       "                              ⎥  ⎢       b_{0}                                \n",
       "                              ⎥, ⎢                                            \n",
       "  -c_{{0}{1}}                 ⎥  ⎢                                c_{{1}{1}}  \n",
       "──────────────────────────────⎥  ⎢  0      0    ──────────────────────────────\n",
       "1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}⎥  ⎢              c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}\n",
       "                              ⎥  ⎢                                            \n",
       "   c_{{0}{0}}                 ⎥  ⎢                               -c_{{1}{0}}  \n",
       "──────────────────────────────⎥  ⎢  0      0    ──────────────────────────────\n",
       "1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}⎦  ⎣              c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}\n",
       "\n",
       "                                                              ⎤⎞\n",
       "                                       0                      ⎥⎟\n",
       "                                                              ⎥⎟\n",
       "                                                              ⎥⎟\n",
       "                                                              ⎥⎟\n",
       "                                       0                      ⎥⎟\n",
       "                                                              ⎥⎟\n",
       "                                                              ⎥⎟\n",
       "                                  -c_{{0}{1}}                 ⎥⎟\n",
       "───────────────  ─────────────────────────────────────────────⎥⎟\n",
       "{1}}⋅c_{{1}{0}}  c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}⎥⎟\n",
       "                                                              ⎥⎟\n",
       "                                   c_{{0}{0}}                 ⎥⎟\n",
       "───────────────  ─────────────────────────────────────────────⎥⎟\n",
       "{1}}⋅c_{{1}{0}}  c_{{0}{0}}⋅c_{{1}{1}} - c_{{0}{1}}⋅c_{{1}{0}}⎦⎠"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs.inv(),sp.diag(*[i.inv() for i in xa])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs.inv()==sp.diag(*[i.inv() for i in xa])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
